package cc.iits.demo.crane.web.controller;

import cc.iits.demo.crane.api.bo.biz.HistoryBO;
import cc.iits.demo.crane.api.service.biz.HistoryService;
import cc.iits.demo.crane.api.vo.RestResponse;
import cc.iits.demo.crane.web.support.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/v1/history")
public class HistoryController {

    @Autowired
    private HistoryService service;

    @PostMapping("/save")
    public RestResponse save(
            @RequestAttribute(Constants.SESSION_ID) String sid,
            @RequestBody HistoryBO history
    ) {
        if (history.getLogger() != null && history.getLogger() == true) {
            history.setSid(sid);
            service.insert(history);
        }
        return RestResponse.success();
    }

    @PostMapping("/list")
    public RestResponse list(
            @RequestAttribute(Constants.SESSION_ID) String sid,
            @RequestBody HistoryBO params
    ) {
        params.setSid(sid);
        return RestResponse.success(service.select(params));
    }
}
